//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package utils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeUtils {
    public static String DATE_MONTH_TIME = "MMdd";
    public static String DATE_FORMAT_TIME = "HH:mm:ss";
    public static String DATE_FORMAT_SHORT_TIME = "HHmmss";
    public static String DATE_FORMAT_TIME_SHORT = "HH:mm";
    public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd";
    public static String DATE_FORMAT_DATEPOINT = "yyyy.MM.dd";
    public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_FORMAT_DATEMIN = "yyyy-MM-dd HH:mm";
    public static String DATE_FORMAT_DATETIME_SHORT = "yyyyMMddHHmmss";
    public static String DATE_FORMAT_DATETIME_SHORT_MI = "yyyyMMddHHmmsssss";
    public static String DATE_FORMAT_DATEANDMON = "yyyyMM";
    public static String DATE_FORMAT_DATEANDMON_BS = "yyyy-MM";
    public static String DATE_FORMAT_YEARDATEANDMON = "yyyyMMdd";
    public static String DATE_FORMAT_YEARDATEANDMON_BS = "yyyy/MM/dd";
    public static String DATE_FORMAT_DATETIME_BS = "yyyy/MM/dd HH:mm:ss";
    public static String startDayTime = " 00:00:00";
    public static String endDayTime = " 23:59:59";

    public TimeUtils() {
    }

    public static void main(String[] args) {
        System.err.println(formatTime(new Date(), DATE_FORMAT_DATEPOINT));
    }

    public static int getDayOfMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(5);
    }

    public static Date getDayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        Date start = calendar.getTime();
        return start;
    }

    public static Date getDayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.add(5, 1);
        calendar.set(13, -1);
        Date end = calendar.getTime();
        return end;
    }

    public static Map<String, Date> getBetweenTommorowDay(int day) {
        Map map = new HashMap();
        Calendar c = Calendar.getInstance();
        int MaxDay = c.getActualMaximum(5);
        if (day > MaxDay) {
            return null;
        } else {
            c.set(c.get(1), c.get(2), day - 1, 0, 0, 0);
            map.put("start", new Date(c.getTimeInMillis()));
            c.set(c.get(1), c.get(2), day + 1, 0, 0, 0);
            map.put("end", new Date(c.getTimeInMillis()));
            return map;
        }
    }

    public static Map<String, Date> getBetweenDay(int day) {
        Map map = new HashMap();
        Calendar c = Calendar.getInstance();
        int MaxDay = c.getActualMaximum(5);
        if (day > MaxDay) {
            return null;
        } else {
            c.set(c.get(1), c.get(2), day, 0, 0, 0);
            map.put("start", new Date(c.getTimeInMillis()));
            c.set(c.get(1), c.get(2), day + 1, 0, 0, 0);
            map.put("end", new Date(c.getTimeInMillis()));
            return map;
        }
    }

    public static String getMondayOfThisWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(7) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }

        c.add(5, -day_of_week + 1);
        return sdf.format(c.getTime());
    }

    public static String getSundayOfThisWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(7) - 1;
        if (day_of_week == 0) {
            day_of_week = 7;
        }

        c.add(5, -day_of_week + 7);
        return sdf.format(c.getTime());
    }

    public static String getFristDayOfMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
        Calendar c = Calendar.getInstance();
        c.add(2, 0);
        c.set(5, 1);
        return sdf.format(c.getTime());
    }

    public static String getLastDayOfMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
        Calendar ca = Calendar.getInstance();
        ca.set(5, ca.getActualMaximum(5));
        return sdf.format(ca.getTime());
    }

    public static Timestamp getTomDay() {
        return getAfterDay(1);
    }

    public static Timestamp getAfterDay(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(11, 0);
        calendar.add(5, day);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    public static Timestamp getAfterDay(int day, String startTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse(startTime));
        calendar.add(5, day);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    public static Timestamp getNextMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(2, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    public static int isSameDay(long nowTime, long buildTime) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(nowTime);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTimeInMillis(buildTime);
        boolean isSameYear = cal1.get(1) == cal2.get(1);
        boolean isSameMonth = isSameYear && cal1.get(2) == cal2.get(2);
        boolean isSameDate = isSameMonth && cal1.get(5) == cal2.get(5);
        if (isSameDate) {
            return 0;
        } else {
            int day1 = cal1.get(6);
            int day2 = cal2.get(6);
            int year1 = cal1.get(1);
            int year2 = cal2.get(1);
            if (year1 == year2) {
                return day1 - day2;
            } else {
                int timeDistance = 0;

                for(int i = year1; i < year2; ++i) {
                    if ((i % 4 != 0 || i % 100 == 0) && i % 400 != 0) {
                        timeDistance += 365;
                    } else {
                        timeDistance += 366;
                    }
                }
                return timeDistance + (day1 - day2);
            }
        }
    }

    public static boolean isBetweenTime(long startTime, long endTime, long nowTime) {
        if (startTime == 0L && endTime == 0L) {
            return false;
        } else {
            if (startTime != 0L && endTime == 0L) {
                if (nowTime >= startTime) {
                    return true;
                }
            } else if (startTime == 0L && endTime != 0L) {
                if (nowTime <= endTime) {
                    return true;
                }
            } else if (nowTime >= startTime && nowTime <= endTime) {
                return true;
            }

            return false;
        }
    }

    public static String getWeekOfDate(String time) {
        String[] weekDays = new String[]{"7", "1", "2", "3", "4", "5", "6"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse(DATE_FORMAT_DATEONLY, time));
        int w = cal.get(7) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    public static List<String> getCurrentWeekDays() {
        List<String> weekDays = new ArrayList();
        Calendar calendar = Calendar.getInstance();

        while(calendar.get(7) != 2) {
            calendar.add(5, -1);
        }

        for(int i = 0; i < 7; ++i) {
            weekDays.add(formatTime(calendar.getTime(), DATE_FORMAT_DATEONLY));
            calendar.add(5, 1);
        }

        return weekDays;
    }

    public static List<String> getweek(int n) {
        ArrayList date = new ArrayList();

        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(2);
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            int dayWeek = cal.get(7);
            if (1 == dayWeek) {
                cal.add(5, -1);
            }

            cal.setFirstDayOfWeek(2);
            int day = cal.get(7);
            cal.add(5, cal.getFirstDayOfWeek() - day + 7 * n);
            date.add(formatTime(cal.getTime(), DATE_FORMAT_DATEONLY));

            for(int i = 1; i < 7; ++i) {
                cal.add(5, 1);
                date.add(formatTime(cal.getTime(), DATE_FORMAT_DATEONLY));
            }

            return date;
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }
    }

    public static int getAge(String birthDay) {
        try {
            Calendar cal = Calendar.getInstance();
            if (cal.before(birthDay)) {
                throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
            } else {
                int yearNow = cal.get(1);
                int monthNow = cal.get(2);
                int dayOfMonthNow = cal.get(5);
                SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
                cal.setTime(sdf.parse(birthDay));
                int yearBirth = cal.get(1);
                int monthBirth = cal.get(2);
                int dayOfMonthBirth = cal.get(5);
                int age = yearNow - yearBirth;
                if (monthNow <= monthBirth) {
                    if (monthNow == monthBirth) {
                        if (dayOfMonthNow < dayOfMonthBirth) {
                            --age;
                        }
                    } else {
                        --age;
                    }
                }

                return age;
            }
        } catch (Exception var10) {
            return 0;
        }
    }

    public static Date parse(String formate, String str) {
        try {
            SimpleDateFormat mate = new SimpleDateFormat(formate);
            return mate.parse(str);
        } catch (ParseException var3) {
            var3.printStackTrace();
            return new Date();
        }
    }

    public static SimpleDateFormat getFormat(String pattern) {
        return new SimpleDateFormat(pattern);
    }

    public static String formatTime(Date date, String patternStr) {
        if (Objects.nonNull(date) && StringUtils.isNotEmpty(patternStr)) {
            SimpleDateFormat simpleDateFormat = getFormat(patternStr);
            return simpleDateFormat.format(date);
        } else {
            return null;
        }
    }

    public static int compDate(String str) throws ParseException {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return date.compareTo(sd.parse(str));
    }

    public static Date parse(String str) {
        Date date = null;
        if (str == null) {
            return null;
        } else {
            try {
                date = (new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US)).parse(str);
            } catch (Exception var7) {
            }

            if (date == null) {
                try {
                    if (str.contains("-") && str.contains(":")) {
                        date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(str);
                    } else {
                        date = (new SimpleDateFormat("yyyy-MM-dd")).parse(str);
                    }
                } catch (ParseException var9) {
                }
            }

            if (null == date) {
                try {
                    if (str.contains("/") && str.contains(":")) {
                        date = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).parse(str);
                    } else {
                        date = (new SimpleDateFormat("yyyy/MM/dd")).parse(str);
                    }
                } catch (Exception var8) {
                }
            }

            if (null == date) {
                try {
                    date = (new SimpleDateFormat("yyyyMMdd")).parse(str);
                } catch (Exception var6) {
                }
            }

            SimpleDateFormat format;
            if (date == null) {
                format = new SimpleDateFormat("HH:mm:ss");

                try {
                    date = format.parse(str);
                } catch (ParseException var5) {
                }
            }

            if (date == null) {
                format = new SimpleDateFormat("HH:mm");

                try {
                    date = format.parse(str);
                } catch (ParseException var4) {
                }
            }

            return date;
        }
    }

    public static class TimeSearch {
        public static final String START_TIME = "start";
        public static final String END_TIME = "end";

        public TimeSearch() {
        }
    }
}
